# -*- encoding: utf-8 -*-
from event_rule.dwc import NO_BET_AMOUNT_GAME_TYPE, NO_WIN_AMOUNT_GAME_TYPE

PRODUCT_PROFIT_CONFIG = {
    'real_time_report': False,
    'interval_type_list': ['day', 'week', 'month'],
    'target_event_id': ['bet_order', 'recharge', 'withdraw', 'active'],
    'values': [
        {
            "value_id": "recharge_amount",
            "target_value": "recharge_price",
            "value_name": "充值金额",
            "filter": [
                {'term': {'_event_id': 'recharge'}}
            ],
            "value_type": "float",
            "adjust_amount": True,
        },
        {
            "value_id": "withdraw_amount",
            "target_value": "withdraw_real_price",
            "value_name": "提现金额",
            "filter": [
                {'term': {'_event_id': 'withdraw'}}
            ],
            "value_type": "float",
            "adjust_amount": True,
        },
        {
            "value_id": "financial_profit_amount",
            "target_value": ["recharge_price", "-", "withdraw_real_price"],
            "value_name": "财务营收",
            "filter": [
                {'terms': {'_event_id': ['recharge', 'withdraw']}},
            ],
            "value_type": "float",
            "adjust_amount": True,
            "description": "充值金额 - 提现金额"
        },
        {
            "value_id": "financial_profit_margin",
            "value_name": "利润率",
            "value_expr": "financial_profit_amount/recharge_amount",
            "is_percentage": True,
            "fmt": lambda v: float("%.2f" % (v * 100)),
            "description": "财务营收 / 充值金额"
        },
        {
            "value_id": "_total_bet_amount",
            "target_value": "bet_order_bet_amount",
            "value_name": "投注金额",
            "filter": [
                {'term': {'_event_id': 'bet_order'}}
            ],
            "value_type": "float",
            "adjust_amount": True,
            "hide_result": True
        },
        {
            "value_id": "total_bet_amount",
            "value_name": "投注金额",
            "value_expr": "_total_bet_amount - lose_award_amount",
            "adjust_amount": True,
            "description": "游戏投注金额总和",
        },
        {
            "value_id": "bet_profit_amount",
            "target_value": ["bet_order_bet_amount", "-", "bet_order_award_amount"],
            "value_name": "玩法营收",
            "filter": [
                {'term': {'_event_id': 'bet_order'}}
            ],
            "value_type": "float",
            "adjust_amount": True,
            "description": "投注金额 - 中奖金额"
        },
        {
            "value_id": "tax_amount",
            "target_value": "bet_order_tax_amount",
            "value_name": "玩法税收",
            "filter": [
                {"term": {'_event_id': 'bet_order'}}
            ],
            "value_type": "float",
            "adjust_amount": True,
        },

        {
            "value_id": "lose_award_amount",
            "target_value": "bet_order_award_amount",
            "value_name": "负扣金额",
            "filter": [
                {"terms": {"bet_order_sub_type.keyword": NO_BET_AMOUNT_GAME_TYPE}},
                {"range": {"bet_order_award_amount": {"lt": 0}}}
            ],
            "value_type": "float",
            "hide_result": True,
            "adjust_amount": True,
        },
        {
            "value_id": "invalid_bet_amount",
            "target_value": "bet_order_bet_amount",
            "value_name": "无效投注",
            "filter": [
                {"terms": {"bet_order_sub_type.keyword": NO_WIN_AMOUNT_GAME_TYPE}},
                {"range": {"bet_order_award_amount": {"gt": 0}}}
            ],
            "value_type": "float",
            "hide_result": True,
            "adjust_amount": True,
        },
        {
            "value_id": "valid_bet_amount",
            "value_name": "有效投注",
            "value_expr": "_total_bet_amount - invalid_bet_amount + (lose_award_amount * -1)",
            "hide_result": True,
            "adjust_amount": True,
        },

        {
            "value_id": "bet_profit_margin",
            "value_name": "抽水率",
            "value_expr": "bet_profit_amount/valid_bet_amount",
            "is_percentage": True,
            "fmt": lambda v: float("%.2f" % (v * 100)),
            "description": "玩法营收 / 有效投注金额"
        },
        {
            "value_id": "platform_award",
            "target_value": "platform_award_amount",
            "value_name": "系统奖励",
            "filter": [
                {'term': {'_event_id': 'platform_award'}},
            ],
            "not_filter": [
                {'term': {'platform_reward_from': 'agent'}}
            ],
            "value_type": "float",
            "adjust_amount": True,
            "description": "系统奖励金额"
        },
        {
            "value_id": "real_profit_amount",
            "value_name": "系统营收",
            "value_expr": "bet_profit_amount - platform_award",
            "value_type": "float",
            "adjust_amount": True,
            "description": "玩法营收 - 系统奖励"
        },
        {
            "value_id": "real_profit_margin",
            "value_name": "系统抽水率",
            "value_expr": "real_profit_amount/valid_bet_amount",
            "value_type": "float",
            "is_percentage": True,
            "fmt": lambda v: float("%.2f" % (v * 100)),
            "description": "系统营收 / 有效投注金额"
        }
    ]
}
